//
//  MainViewController.m
//  XiaoHongShu
//
//  Created by Jason on 2022/6/15.
//

#import "MainViewController.h"
#import "HMSegmentedControl.h"
#import "FindViewController.h"
#import "FocusViewController.h"
#import "NearViewController.h"




@interface MainViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *searchBtn;
@property (nonatomic, strong) HMSegmentedControl *segmentControl;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray<UIViewController *> *subVCArr;



@end

@implementation MainViewController

-(NSMutableArray<UIViewController *> *)subVCArr {
    if (!_subVCArr) {
       _subVCArr = [[NSMutableArray alloc] init];

        //关注
        FocusViewController *focusVC = [[FocusViewController alloc] init];
        [_subVCArr addObject:focusVC];
        
        //发现
        FindViewController *findVC = [[FindViewController alloc] init];
        [_subVCArr addObject:findVC];
        
        //附近
        NearViewController *nearVC = [[NearViewController alloc] init];
        [_subVCArr addObject:nearVC];
    }
    return _subVCArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    
    
    self.navigationItem.titleView = self.segmentControl;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:kImageName(@"yuanquan") style:UIBarButtonItemStyleDone target:self action:@selector(leftItemClick)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:kImageName(@"sousuo") style:UIBarButtonItemStyleDone target:self action:@selector(rightitemClick)];
    
    [self setupFrame];
    
}

#pragma mark - 懒加载segmentControl
-(HMSegmentedControl *)segmentControl {
    if (!_segmentControl) {
        _segmentControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"关注", @"发现", @"附近"]];
        _segmentControl.frame = CGRectMake((kSPScreenWidth - 150) / 2, (kNavContentBarHeight - 25) / 2, 150, 25);
        _segmentControl.segmentEdgeInset = UIEdgeInsetsMake(0, 5, 0, 5);
        _segmentControl.backgroundColor = kClearColor;
        _segmentControl.titleTextAttributes = @{NSForegroundColorAttributeName :kLightGrayColor ,NSFontAttributeName:[UIFont systemFontOfSize:13.0]};
        if (@available(iOS 13.0, *)) {
            _segmentControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName :[UIColor labelColor] ,NSFontAttributeName:[UIFont systemFontOfSize:14.0]};
        } else {
            _segmentControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName :kBlackColor ,NSFontAttributeName:[UIFont systemFontOfSize:14.0]};
        }
        //横条宽度
        _segmentControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
        //横条出现的位置
        _segmentControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
        //横条高度
        _segmentControl.selectionIndicatorHeight = 2.0f;
        //横条的颜色
        _segmentControl.selectionIndicatorColor = kRedColor;
        //初始选择
        _segmentControl.selectedSegmentIndex = 0;
       // _segmentControl.tag = 3;
        
       // [_segmentControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];

        __weak typeof(self) weakSelf = self;
        _segmentControl.indexChangeBlock = ^(NSInteger index) {
            [weakSelf.scrollView scrollRectToVisible:CGRectMake(kSPScreenWidth * index, 0, kSPScreenWidth, weakSelf.scrollView.bounds.size.height) animated:YES];
        };
    }
    return _segmentControl;
}

#pragma mark - 懒加载scrollView
-(UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.bounces = NO;
        _scrollView.backgroundColor = kYellowColor;
        _scrollView.pagingEnabled = YES;
        _scrollView.alwaysBounceHorizontal = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        if (@available(iOS 11.0, *)) {
            _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
        }

//        NSInteger selectIndex = self.segmentControl.selectedSegmentIndex;
//        [self.scrollView scrollRectToVisible:CGRectMake(selectIndex * kSPScreenWidth, 0, kSPScreenWidth, kSPScreenHeight) animated:NO];
    }
    return _scrollView;
}

#pragma mark - 设置导航栏上控件的frame
- (void)setupFrame{
    
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(self.view);
    }];
    
    [self.scrollView layoutIfNeeded];
    self.scrollView.contentSize = CGSizeMake(kSPScreenWidth * self.subVCArr.count, 1);
    NSLog(@"count = %ld",self.subVCArr.count);

    NSLog(@"%f:%f", self.scrollView.frame.size.height, kSPScreenHeight);

    NSLog(@"subVCArr = %@", self.subVCArr);
    for (int i = 0; i < self.subVCArr.count; i++) {
        [[self.subVCArr[i] view] setFrame:CGRectMake(i * kSPScreenWidth, 0, kSPScreenWidth, self.scrollView.frame.size.height)];
        [self addChildViewController:self.subVCArr[i]];
        [self.scrollView addSubview:[self.subVCArr[i] view]];
    }
    
}


- (void)leftItemClick {
    NSLog(@"点击左边按钮");
}

- (void)rightitemClick {
    NSLog(@"点击搜索按钮");
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    [self.segmentControl setSelectedSegmentIndex:page animated:YES];
    NSLog(@"pageWidth = %f - page = %ld",pageWidth, page);
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
